Imports System
Imports System.Text
Imports System.Data
Imports System.Collections.Generic

Imports Framework



'Table-Row Class (Customisable half)
Partial Public Class CFile

#Region "Constants"
#End Region

#Region "Constructors (Public)"
    'Default Connection String
    Public Sub New()
        MyBase.New()
    End Sub
    
    'Alternative Connection String
    Public Sub New(ByVal dataSrc as CDataSrc)
        MyBase.New(dataSrc)
    End Sub
    
    'Hidden (UI code should use cache instead)
    Protected Friend Sub New([fileId] As Integer)
        MyBase.New([fileId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, [fileId] As Integer)
        MyBase.New(dataSrc, [fileId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, ByVal [fileId] As Integer, ByVal txOrNull As IDbTransaction)
        MyBase.New(dataSrc, [fileId], txOrNull)
    End Sub
#End Region

#Region "Default Values"
    Protected Overrides Sub InitValues_Custom()
        'm_sampleDateCreated = DateTime.Now
        'm_sampleSortOrder   = 0
    End Sub 
#End Region

#Region "Default Connection String"
    Protected Overrides Function DefaultDataSrc() As CDataSrc
        Return CDataSrc.Default
    End Function
#End Region

#Region "Properties - Relationships"    
    'Relationships - Foriegn Keys (e.g parent)
    Public ReadOnly Property [Product]() As CProduct
        Get
            Return CProduct.Cache.GetById(Me.FileProductId)
        End Get
    End Property
    Public ReadOnly Property [Extension]() As CExtension
        Get
            Return CExtension.Cache.GetById(Me.FileExtensionId)
        End Get
    End Property

    'Relationships - Collections (e.g. children)

#End Region

#Region "Properties - Customisation"

    Public Property [FileExtensionId]() As Byte
        Get
            If 0 = m_fileExtensionId Then
                Dim lastDot As Integer = m_fileName.LastIndexOf(".")
                If -1 <> lastDot Then
                    Dim ext As String = m_fileName.Substring(lastDot + 1).ToLower
                    For Each i As CExtension In CExtension.Cache
                        For Each j As String In CUtilities.StringToListStr(i.ExtensionLookFor.ToLower)
                            If j = ext Then
                                m_fileExtensionId = i.ExtensionId
                                Return m_fileExtensionId
                            End If
                        Next
                    Next
                End If
            End If
            Return m_fileExtensionId
        End Get
        Set(ByVal value As Byte)
            m_fileExtensionId = value
        End Set
    End Property

    Public ReadOnly Property FileExtensionId_ As EExtension
        Get
            Return FileExtensionId
        End Get
    End Property
    Public ReadOnly Property IsPdfOrWord
        Get
            Return FileExtensionId_ = EExtension.PdfOrWord
        End Get
    End Property
    Public ReadOnly Property IsMp3
        Get
            Return FileExtensionId_ = EExtension.Mp3
        End Get
    End Property
    Public ReadOnly Property IsHtm
        Get
            Return FileExtensionId_ = EExtension.Html
        End Get
    End Property
    Public ReadOnly Property IsMobi
        Get
            Return FileExtensionId_ = EExtension.mobi
        End Get
    End Property
    Public ReadOnly Property IsEpub
        Get
            Return FileExtensionId_ = EExtension.epub
        End Get
    End Property


    Public ReadOnly Property FileNameAndSize As String
        Get
            If FileSize = 0 Then Return IO.Path.GetFileName(FileName)
            Try
                Return String.Concat(IO.Path.GetFileName(FileName), " (", CUtilities.FileSize(FileSize), ")")
            Catch
                Return String.Concat(FileNameShort, " (", CUtilities.FileSize(FileSize), ")")
            End Try
        End Get
    End Property
    Public ReadOnly Property FileNameShort As String
        Get
            Dim s As String = FileName
            Try
                s = IO.Path.GetFileName(FileName)
            Catch
            End Try
            If s.Length < 50 Then Return s
            Return String.Concat(s.Substring(0, 33), "...", s.Substring(s.Length - 14, 14))
        End Get
    End Property
    Public ReadOnly Property FileNameAndSizeShort As String
        Get
            If FileSize = 0 Then Return FileNameShort
            Return String.Concat(FileNameShort, " (", CUtilities.FileSize(FileSize), ")")
        End Get
    End Property
#End Region

#Region "Save/Delete Overrides"
     'Can Override MyBase.Save/Delete (e.g. Cascade deletes, or insert related records)
#End Region

#Region "Custom Database Queries"
    '(Not normally required for cached classes, use list class for searching etc)
    'For Stored Procs can use: MakeListTyped (matching schema), or DataSrc.ExecuteDataset (reports etc)
    'For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
#End Region

#Region "Searching (Optional)"
    'For cached classes, custom seach logic resides in static methods on the list class
    ' e.g. CFile.Cache.Search("..")

    'See also the auto-generated methods based on indexes
    ' e.g. CFile.Cache.GetBy...
#End Region

#Region "Caching Details"
    'Cache Key
    Friend Shared CACHE_KEY As String = GetType(CFile).ToString 'TABLE_NAME

    'Cache data
    Private Shared Function LoadCache() As CFileList
        Return New CFile().SelectAll()
    End Function
    
    'Cache Timeout
    Private Shared Sub SetCache(ByVal value As CFileList)
        If Not IsNothing(value) Then value.Sort()
        CCache.Set(CACHE_KEY, value) 'Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
    End Sub
    
    'Helper Method
    Private Function CacheGetById(ByVal list As CFileList) As CFile
        Return list.GetById(Me.FileId)
    End Function
#End Region

#Region "Cloning"
    Public Function Clone(ByVal target as CDataSrc, ByVal txOrNull as IDbTransaction) As CFile ', parentId As Integer) As CFile
        'Shallow copy: Copies the immediate record, excluding autogenerated Pks
        Dim copy As New CFile(Me, target)

        'Deep Copy - Child Entities: Cloned children must reference their cloned parent
        'copy.SampleParentId = parentId

        copy.Save(txOrNull)

        'Deep Copy - Parent Entities: Cloned parents also clone their child collections
        'Me.Children.Clone(target, txOrNull, copy.FileId)

        Return copy
    End Function
#End Region

#Region "ToXml"
    Protected Overrides Sub ToXml_Custom(ByVal w As System.Xml.XmlWriter)
        'Store(w, "Example", Me.Example)
    End Sub
#End Region

End Class
